﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using BOMS.DBUtility;//请先添加引用
namespace BOMS.DAL
{
    /// <summary>
    /// 数据访问类S_BatSnapshot。
    /// </summary>
    public class S_BatSnapshot
    {
        public S_BatSnapshot()
        { }
        #region  成员方法
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int ID, int Generation, string EquipID, string BatteryGroup, int BatteryID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from S_BatSnapshot");
            strSql.Append(" where ID=@ID and Generation=@Generation and EquipID=@EquipID and BatteryGroup=@BatteryGroup and BatteryID=@BatteryID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4),
					new SqlParameter("@Generation", SqlDbType.TinyInt),
					new SqlParameter("@EquipID", SqlDbType.NVarChar,50),
					new SqlParameter("@BatteryGroup", SqlDbType.NVarChar,50),
					new SqlParameter("@BatteryID", SqlDbType.Int,4)};
            parameters[0].Value = ID;
            parameters[1].Value = Generation;
            parameters[2].Value = EquipID;
            parameters[3].Value = BatteryGroup;
            parameters[4].Value = BatteryID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public BOMS.Model.S_BatSnapshot GetModel(int ID, int Generation, string EquipID, string BatteryGroup, int BatteryID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 ID,Generation,EquipID,BatteryGroup,BatteryID,Vval,Ival,Tval,Status,LogTime,IsNew from S_BatSnapshot ");
            strSql.Append(" where ID=@ID and Generation=@Generation and EquipID=@EquipID and BatteryGroup=@BatteryGroup and BatteryID=@BatteryID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4),
					new SqlParameter("@Generation", SqlDbType.TinyInt),
					new SqlParameter("@EquipID", SqlDbType.NVarChar,50),
					new SqlParameter("@BatteryGroup", SqlDbType.NVarChar,50),
					new SqlParameter("@BatteryID", SqlDbType.Int,4)};
            parameters[0].Value = ID;
            parameters[1].Value = Generation;
            parameters[2].Value = EquipID;
            parameters[3].Value = BatteryGroup;
            parameters[4].Value = BatteryID;

            BOMS.Model.S_BatSnapshot model = new BOMS.Model.S_BatSnapshot();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    model.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Generation"].ToString() != "")
                {
                    model.Generation = int.Parse(ds.Tables[0].Rows[0]["Generation"].ToString());
                }
                model.EquipID = ds.Tables[0].Rows[0]["EquipID"].ToString();
                model.BatteryGroup = ds.Tables[0].Rows[0]["BatteryGroup"].ToString();
                if (ds.Tables[0].Rows[0]["BatteryID"].ToString() != "")
                {
                    model.BatteryID = int.Parse(ds.Tables[0].Rows[0]["BatteryID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Vval"].ToString() != "")
                {
                    model.Vval = decimal.Parse(ds.Tables[0].Rows[0]["Vval"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Ival"].ToString() != "")
                {
                    model.Ival = decimal.Parse(ds.Tables[0].Rows[0]["Ival"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Tval"].ToString() != "")
                {
                    model.Tval = decimal.Parse(ds.Tables[0].Rows[0]["Tval"].ToString());
                }
                model.Status = ds.Tables[0].Rows[0]["Status"].ToString();
                if (ds.Tables[0].Rows[0]["LogTime"].ToString() != "")
                {
                    model.LogTime = DateTime.Parse(ds.Tables[0].Rows[0]["LogTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["IsNew"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["IsNew"].ToString() == "1") || (ds.Tables[0].Rows[0]["IsNew"].ToString().ToLower() == "true"))
                    {
                        model.IsNew = true;
                    }
                    else
                    {
                        model.IsNew = false;
                    }
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,Generation,EquipID,BatteryGroup,BatteryID,Vval,Ival,Tval,Status,LogTime,IsNew ");
            strSql.Append(" FROM S_BatSnapshot ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" ID,Generation,EquipID,BatteryGroup,BatteryID,Vval,Ival,Tval,Status,LogTime,IsNew ");
            strSql.Append(" FROM S_BatSnapshot ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "S_BatSnapshot";
            parameters[1].Value = "ID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  成员方法
    }
}

